/*
 * @File name:
 * @Author: 1079161148@qq.com
 * @Version: V1.0
 * @Date: 2025-02-28 14:27:46
 * @Description:
 * Copyright (C) 2024-{year} Tsing Micro Technology Inc All rights reserved.
 */

import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import NProgress from '@/utils/nprogress'
import { TokenService } from '@/utils/token'
import { useUserStore } from '@/stores/user'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/chat',
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      public: true, // 公开路由
    },
  },
  {
    path: '/chat',
    name: 'Chat',
    component: () => import('@/views/chat/index.vue'),
  },
  {
    path: '/chat/detail/:id',
    name: 'ChatDetail',
    component: () => import('@/views/chat/ChatDetail.vue'),
  },
  {
    path: '/contacts',
    name: 'Contacts',
    component: () => import('@/views/contacts/index.vue'),
  },
  {
    path: '/discover',
    name: 'Discover',
    component: () => import('@/views/discover/index.vue'),
  },
  {
    path: '/me',
    name: 'Me',
    component: () => import('@/views/me/index.vue'),
    meta: {
      title: '我的',
    },
  },
  {
    path: '/deepseek',
    name: 'DeepSeek',
    component: () => import('@/views/deepseek/index.vue'),
    meta: {
      title: 'DeepSeek AI',
    },
  },
  {
    path: '/friend',
    name: 'Friend',
    component: () => import('@/views/friend/index.vue'),
    meta: {
      title: '朋友圈',
    },
  },
  {
    path: '/moments',
    name: 'Moments',
    component: () => import('@/views/moments/index.vue'),
    meta: {
      title: '看一看',
    },
  },
  {
    path: '/moments/:id',
    component: () => import('@/views/moments/detail.vue'),
    meta: { title: '动态详情' },
  },
  {
    path: '/scan',
    name: 'Scan',
    component: () => import('@/views/scan/index.vue'),
    meta: {
      title: '扫一扫记录',
    },
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/search/index.vue'),
    meta: {
      title: '搜一搜',
    },
  },
  {
    path: '/album',
    name: 'Album',
    component: () => import('@/views/album/index.vue'),
    meta: {
      title: '我的相册',
    },
  },
  {
    path: '/live',
    name: 'Live',
    component: () => import('@/views/live/index.vue'),
    meta: {
      title: '直播和附近',
    },
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 路由守卫
router.beforeEach((to, _from, next) => {
  NProgress.start()
  const token = TokenService.getToken()
  const userStore = useUserStore()

  // 检查是否需要登录
  if (!to.meta.public && !token) {
    next({
      path: '/login',
      query: { redirect: to.fullPath },
    })
    return
  }

  // 已登录时访问登录页，重定向到首页或指定页面
  if (to.path === '/login' && token) {
    next((to.query.redirect as string) || '/')
    return
  }

  // 如果有 token 但没有用户信息，尝试获取用户信息
  if (token && !userStore.userInfo) {
    userStore.getUserInfo().catch(() => {
      userStore.logout()
      next('/login')
      return
    })
  }

  next()
})

// 路由后置守卫
router.afterEach(() => {
  // 结束进度条
  NProgress.done()
})

// 路由错误处理
router.onError(() => {
  NProgress.done()
})

export default router
